import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_page/mine_business/collec_history_buy/col_his_buy_page.dart';
import 'package:tiktokadult/hj_page/mine_business/collec_history_buy/col_hit_buy_sub_controller.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/tools_base/refresh/pull_refresh.dart';
import 'package:tiktokadult/hj_utils/history_record_util.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';

enum LoadDataType {
  /// 长视频
  video(PageStyle.grid, 'SP', 2, 12, 8, EdgeInsets.only(left: 20, right: 20, top: 14), 174 / 160, 'SP', HistoryRecordType.video),

  /// 短视频
  short(PageStyle.grid, 'SHORT', 2, 12, 6, EdgeInsets.only(left: 20, right: 20, top: 14), 3 / 4, 'SHORT', HistoryRecordType.short),

  /// 漫画 media_bookshelf/list, media/my_buy
  comics(PageStyle.grid, 'image', 3, 12, 6, EdgeInsets.only(left: 16, right: 16, top: 14), 115 / 192, 'image', HistoryRecordType.comics),

  /// 动漫  media_bookshelf/list, media/my_buy
  cartoon(PageStyle.grid, 'video', 3, 12, 6, EdgeInsets.only(left: 16, right: 16, top: 14), 115 / 192, 'video', HistoryRecordType.cartoon),

  /// 小说   media_bookshelf/list, media/my_buy
  novel(PageStyle.grid, 'text', 3, 12, 6, EdgeInsets.only(left: 16, right: 16, top: 14), 115 / 192, 'text', HistoryRecordType.novel),

  /// 帖子
  post(PageStyle.list, 'COVER', 2, 10, 14, EdgeInsets.only(left: 0, right: 0, top: 14), 191 / 174, 'COVER', HistoryRecordType.post),

  /// 图集
  pictures(PageStyle.grid, 'PIC', 2, 10, 14, EdgeInsets.only(left: 20, right: 20, top: 14), 3 / 4, 'PIC', HistoryRecordType.picture),

  /// 种子/黄油帖子
  game(
      PageStyle.grid, 'SEED_LINK', 2, 12, 8, EdgeInsets.only(left: 20, right: 20, top: 14), 174 / 160, 'SEED_LINK', HistoryRecordType.game),

  /// 合集
  collections(
      PageStyle.list, 'collection', 2, 12, 6, EdgeInsets.only(left: 16, right: 16, top: 14), 115 / 175, '', HistoryRecordType.cartoon);

  /// 页面风格 列表、瀑布流、网格
  final PageStyle style;

  /// 收藏类型
  final String collectType;

  /// 针对网格 纵向多少个
  final int crossAxisCount;

  /// 瀑布流和网格 垂直间隔
  final double spaceV;

  /// 瀑布流和网格 横向间隔
  final double spaceH;

  /// 页面间距
  final EdgeInsets padding;

  /// 网格宽高比
  final double ratio;

  /// 购买类型
  final String buyType;

  ///历史记录类型
  final HistoryRecordType historyType;

  const LoadDataType(
    this.style,
    this.collectType,
    this.crossAxisCount,
    this.spaceV,
    this.spaceH,
    this.padding,
    this.ratio,
    this.buyType,
    this.historyType,
  );

  static LoadDataType typeFromIndex(int index) {
    return LoadDataType.values[index];
  }
}

enum PageStyle {
  list,
  grid,
  waterflow;
}

class ColHisBuySubPage extends StatelessWidget {
  final LoadDataType loadType;
  final PageType pageType;
  final bool isEdit;

  const ColHisBuySubPage(
    this.loadType, {
    super.key,
    required this.pageType,
    this.isEdit = false,
  });

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BaseCBHController>(
      global: false,
      init: instanceController(pageType, loadType),
      builder: (controller) {
        return pullYsRefresh(
          onInit: (_) => controller.refreshController = _,
          onRefresh: (_) => controller.fetchPageData(),
          onLoading: (_) => controller.fetchPageData(isRefresh: false),
          child: () {
            if (controller.isLoading) return LoadingCenterWidget();
            if (controller.dataSource.isEmpty) return CErrorWidget();
            return _buildContent(controller);
          }(),
        );
      },
    );
  }

  Widget _buildDeleteIcon(BaseCBHController controller, int index) {
    return GestureDetector(
      onTap: () => controller.cancelCollectHandler(index),
      child: Image.asset('mine_col_delete.png'.mineImgPath, width: 20),
    );
  }

  Widget _buildContent(BaseCBHController controller) {
    switch (loadType.style) {
      case PageStyle.list:
        return ListView.separated(
          separatorBuilder: (_, __) => 14.sizeBoxH,
          itemCount: controller.dataSource.length,
          padding: controller.padding,
          itemBuilder: (BuildContext context, int index) {
            return Stack(
              children: [
                controller.instanceChildItem(index, isEdit: isEdit),
                if (isEdit)
                  Positioned(
                    top: 8,
                    right: 8,
                    child: _buildDeleteIcon(controller, index),
                  ),
              ],
            );
          },
        );
      case PageStyle.grid:
        return GridView.builder(
          padding: controller.padding,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: controller.crossAxisCount,
            mainAxisSpacing: controller.spaceV,
            crossAxisSpacing: controller.spaceH,
            childAspectRatio: controller.ratio,
          ),
          itemCount: controller.dataSource.length,
          itemBuilder: (BuildContext context, int index) {
            return Stack(
              fit: StackFit.expand,
              children: [
                controller.instanceChildItem(index, isEdit: isEdit),
                if (isEdit)
                  Positioned(
                    top: 8,
                    right: 8,
                    child: _buildDeleteIcon(controller, index),
                  ),
              ],
            );
          },
        );
      case PageStyle.waterflow:
        return MasonryGridView.builder(
          padding: controller.padding,
          itemCount: controller.dataSource.length,
          gridDelegate: SliverSimpleGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
          ),
          mainAxisSpacing: controller.spaceV,
          crossAxisSpacing: controller.spaceH,
          itemBuilder: (_, index) {
            return Stack(
              fit: StackFit.expand,
              children: [
                controller.instanceChildItem(index, isEdit: isEdit),
                if (isEdit)
                  Positioned(
                    top: 8,
                    right: 8,
                    child: _buildDeleteIcon(controller, index),
                  ),
              ],
            );
          },
        );
      default:
        return Center(
          child: Text('${loadType.style}'),
        );
    }
  }
}
